//
//  MainTabBarController.swift
//  CSYMicroBlockSina
//
//  Created by 姚彦兆 on 15/11/8.
//  Copyright © 2015年 姚彦兆. All rights reserved.
//

import UIKit

import AFNetworking

class MainTabBarController: UITabBarController {

    //发布微博按钮点击事件
    @objc private func clickCommpentButton(){
    
        print("点击了发布按钮")
        
        let compose = CSYComposePageViewController()
        
//        let compose = ComposeViewController()
        
        let nav = UINavigationController(rootViewController: compose)
        
        presentViewController(nav, animated: true, completion: nil)
    }
    
    private func sendAFNetworkRequest(){
    
        let AFN : AFHTTPSessionManager = AFHTTPSessionManager()
        
        AFN.responseSerializer.acceptableContentTypes?.insert("text/html")
        
        AFN.POST("http://www.weather.com.cn/data/sk/101010100.html", parameters: nil, constructingBodyWithBlock: nil, success: { (dataTask, result) -> Void in
            
            //print(result)
            
            }) { (dataTask, error) -> Void in
                
                //print(error)
                
        }
    
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let customTabBar : CSYTabbar = CSYTabbar()
        
        setValue(customTabBar, forKey: "tabBar")
        
        // Do any additional setup after loading the view.
        
        addChildViewControllers()
        
        customTabBar.commpentBtn.addTarget(self, action: "clickCommpentButton", forControlEvents: UIControlEvents.TouchUpInside)
        
    }

    private func addChildViewControllers() {
        
        //添加四个子控制器
        
        self.addChildViewController(HomeTableViewController(), controllerTitile: "首页", controllerImageName: "tabbar_home")
        self.addChildViewController(MessageTableViewController(), controllerTitile: "消息", controllerImageName: "tabbar_message_center")
        self.addChildViewController(DiscoverTableViewController(), controllerTitile: "发现", controllerImageName: "tabbar_discover")
        self.addChildViewController(ProfileTableViewController(), controllerTitile: "我", controllerImageName: "tabbar_profile")
        
        dispatch_async(dispatch_get_main_queue()) { () -> Void in
            
            
            
            
        }
        
        
        
    }
    
    private func addChildViewController(childController: UIViewController , controllerTitile : String , controllerImageName : String) {
        
        //添加TabBar子控制器及其按钮
        
        let navContoller = CSYBasicNavController(rootViewController: childController)
        
        if childController.isKindOfClass(HomeTableViewController){
        
            if GetUserInfoModel().isLogin{
            
                childController.title = GetUserInfoModel().userName
                
            }
        
        }
        
        childController.tabBarItem.title = controllerTitile
        
        childController.tabBarItem.image = UIImage (named: controllerImageName)
        
//        childController.tabBarItem.selectedImage = UIImage(named: controllerImageName + "_highlighted")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
//        
//        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
        
        addChildViewController(navContoller)
        
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
